package com.systar.activity.report;

import java.io.StringWriter;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import com.systar.activity.report.ActivityData;
import com.systar.activity.report.ActivityDataPool;

public class ActivityDataPoolTest
{
	private static Transformer dataTransformer;

	@BeforeClass
	public static void setupTransformer() throws TransformerConfigurationException, TransformerFactoryConfigurationError
	{
		dataTransformer = TransformerFactory.newInstance().newTransformer(
				new javax.xml.transform.stream.StreamSource(
						ActivityDataPoolTest.class.getResourceAsStream("ActivityStyle.xsl")));
	}

	@Test
	public void testNoActiveNoError() throws Exception
	{
		ActivityDataPool data = new ActivityDataPool();
		data.put("SRC1", new ActivityData("SRC1"));

		StringWriter writer = new java.io.StringWriter();
		data.writeHtmlDataFragment(dataTransformer, writer);
		
		String html = writer.toString();
		checkGeneratedHtml(data, html);
	}
	
	@Test
	public void testActiveNoError() throws Exception
	{
		ActivityDataPool data = new ActivityDataPool();
		data.put("SRC1", new ActivityData("SRC1", true, 0));

		StringWriter writer = new java.io.StringWriter();
		data.writeHtmlDataFragment(dataTransformer, writer);
		
		String html = writer.toString();
		checkGeneratedHtml(data, html);
	}
	
	@Test
	public void testActiveWithError() throws Exception
	{
		ActivityDataPool data = new ActivityDataPool();
		data.put("SRC1", new ActivityData("SRC1", true, 12));

		StringWriter writer = new java.io.StringWriter();
		data.writeHtmlDataFragment(dataTransformer, writer);
		
		String html = writer.toString();
		checkGeneratedHtml(data, html);
	}
	
	private void checkGeneratedHtml(ActivityDataPool data, String html)
	{
		for (ActivityData activity : data.values())
		{
			Assert.assertTrue("Should contain the source name", html.contains(">" + activity.getName() + "<"));
			if (activity.isActive())
				Assert.assertTrue("Should contain the active marker", html.contains("class=\"CellActive\""));
			else
				Assert.assertFalse("Should not contain the active marker", html.contains("class=\"CellActive\""));
			if (activity.hasHotErrors())
				Assert.assertTrue("Should contain the error marker", html.contains("class=\"CellError\""));
			else
				Assert.assertFalse("Should not contain the error marker", html.contains("class=\"CellError\""));
			if (!activity.getErrorMessage().isEmpty())
				Assert.assertTrue("Should contain the error message", html.contains(activity.getErrorMessage()));
		}
	}

}
